﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
//using ESRI.ArcGIS.TOCControl;
using ESRI.ArcGIS.Display;
using PlanningGIS.Framework;

using PlanningGIS.TocContextMenu.form;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.TocContextMenu.Button
{
  
    internal class LayersSymbolByStyle : BaseCommand
    {
        IApplication m_Application = null;
        public LayersSymbolByStyle()
        {
            base.m_caption = "根据符号库符号化";
            base.m_toolTip = base.Caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
        }

        public override void OnClick()
        {
            List<IFeatureLayer> pFeatLyrs = new List<IFeatureLayer>();
            if ((m_Application as IFramework).TOCControl.CustomProperty is ILayer)
            {
                ILayer pLayer = (m_Application as IFramework).TOCControl.CustomProperty as ILayer;
                List<ILayer> lyrs = new List<ILayer>();
                MapHelper.GetAllLayer(pLayer, lyrs);
                foreach (ILayer lyr in lyrs)
                {
                    if (lyr is IFeatureLayer)
                    {
                        pFeatLyrs.Add((IFeatureLayer)lyr);
                    }
                }
            }
            else if ((m_Application as IFramework).TOCControl.CustomProperty is IMap)
            {
                IMap pMap = (m_Application as IFramework).TOCControl.CustomProperty as IMap;
                pFeatLyrs = MapHelper.GetAllLayer<IFeatureLayer>(pMap);
            }

            FrmSymbolSetting frmSymbolSetting = new FrmSymbolSetting(m_Application.StatusBar);
            frmSymbolSetting.FeatureLayers = pFeatLyrs ;

            DialogResult dr = frmSymbolSetting.ShowDialog();

            //符号化成功才刷新，否则就不要刷新了
            if (dr == DialogResult.OK)
            {
                (m_Application as IFramework).MapControl.Refresh(esriViewDrawPhase.esriViewGeography, null, null);
                (m_Application as IFramework).TOCControl.Update();
            }
        }

        public override bool Enabled
        {
            get
            {
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
    
}
